package gui.admin.tablemodel;

import dao.ProductDao;
import dao.ProductDao;
import entity.Product;

import javax.swing.table.DefaultTableModel;
import java.util.List;

public class ProductTableModel extends DefaultTableModel {
    ProductDao productDao = new ProductDao();
    String[] tableHeaders = new String[]{"商品编号","商品名称","商品类别","类别名称","保质期","存放位置","进价","售价","库存数量"};//商品信息表头
    List productList = null;//创建商品列表
    String[][] productData = null;
    public ProductTableModel() throws Exception {
        productList = productDao.getAllProducts();//获取所有的商品信息，放入列表
        productData = new String[productList.size()][tableHeaders.length];//设置二维数组大小
        for (int i = 0; i < productList.size(); i++) {
            Product product = (Product)productList.get(i);
            productData[i][0] = product.getProductId();
            productData[i][1] = product.getProductName();
            productData[i][2] = product.getCategoryId();
            productData[i][3] = product.getCategoryName();
            productData[i][4] = product.getExpiryDate();
            productData[i][5] = product.getStorageLocation();
            productData[i][6] = String.valueOf(product.getPurchasePrice());
            productData[i][7] = String.valueOf(product.getSalePrice());
            productData[i][8] = String.valueOf(product.getStockQuantity());
        }
        this.setDataVector(productData,tableHeaders);
    }
}
